package mil.af.amc.adpetracker.web.actions.Ros;

import mil.af.amc.adpetracker.service.RosService;
import mil.af.amc.adpetracker.web.actions.AbstractAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Action to create a new ROS package
 *
 * @author TSgt Jason Ferguson
 */

public class CreateRosAction extends AbstractAction {

    private static final Log log = LogFactory.getLog(CreateRosAction.class);

    private RosService rosService;

    public void setRosService(RosService rosService) {
        this.rosService = rosService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse) throws Exception {

        DynaActionForm form = (DynaActionForm) actionForm;

        try {
            rosService.createRos(form.getMap());
        } catch (Exception e) {

            log.debug("rosService.createRos() threw Exception", e);

            ActionMessage message = new ActionMessage(e.getClass().getName());
            saveError(httpServletRequest, message);
            
            return actionMapping.getInputForward();
        }

        return actionMapping.findForward("success");
    }
}
